<template>
  <div>
    <el-dialog
      :visible.sync="dialog.add"
      :close-on-click-modal="false"
      title="添加"
      width="80%"
      append-to-body
      @closed="dialogClosed('addForm')"
    >
      <el-form ref="addForm" :model="form" label-width="120px" :rules="validateRules">
        <el-row>
          <el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="8">
            <el-form-item label="配置名称" prop="name">
              <el-input v-model="form.name" placeholder="请输入配置名称" clearable />
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24">
            <el-form-item label="配置内容(JSON)" prop="value">
               <div class="editor-container">
                <json-editor ref="JsonEditorValue" v-model="form.value" ></json-editor>
              </div>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24">
            <el-form-item label="配置注释" prop="comment">
              <div class="editor-container">
                <json-editor ref="JsonEditor" v-model="form.comment" :showBtns="false"></json-editor>
              </div>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="8">
            <el-form-item label="中文说明" prop="description">
              <el-input v-model="form.description" placeholder="请输入中文说明" clearable />
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="8">
            <el-form-item label="允许外部访问" prop="outsideAccess">
              <el-select
                v-model="form.outsideAccess"
                placeholder="请选择允许外部访问"
                clearable
                style="width: 100%;"
              >
                <el-option
                  v-for="item in selects.outsideAccessSelect"
                  :value="item.value"
                  :key="item.value"
                  :label="item.displayValue"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer">
        <el-button @click="dialog.add = false">取消</el-button>
        <el-button type="primary" @click="add" :loading="loading.add">添加</el-button>
      </div>
    </el-dialog>
    <el-dialog
      :visible.sync="dialog.edit"
      :close-on-click-modal="false"
      title="修改"
      width="80%"
      append-to-body
      @closed="dialogClosed('editForm')"
    >
      <el-form ref="editForm" :model="form" label-width="120px" :rules="validateRules">
        <el-row>
          <el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="8">
            <el-form-item label="配置名称" prop="name">
              <el-input v-model="form.name" placeholder="请输入配置名称" clearable />
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24">
            <el-form-item label="配置内容(JSON)" prop="value">
              <div class="editor-container">
                <json-editor ref="JsonEditorValue" v-model="form.value" ></json-editor>
              </div>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24">
            <el-form-item label="配置注释" prop="comment">
              <div class="editor-container">
                <json-editor ref="JsonEditor" v-model="form.comment" ></json-editor>
              </div>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="8">
            <el-form-item label="中文说明" prop="description">
              <el-input v-model="form.description" placeholder="请输入中文说明" clearable />
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="8">
            <el-form-item label="允许外部访问" prop="outsideAccess">
              <el-select
                v-model="form.outsideAccess"
                placeholder="请选择允许外部访问"
                clearable
                style="width: 100%;"
              >
                <el-option
                  v-for="item in selects.outsideAccessSelect"
                  :value="item.value"
                  :key="item.value"
                  :label="item.displayValue"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row v-if="form.name === 'sys_pic_config'">
          <el-col :span="12" v-for="(value, key, index) in form.comment" :key="index">
            <el-form-item :label="value">
              <UploadConfigImage
                v-if="dialog.edit"
                action="/sys-config/admin/upload-img"
                formProp="value"
                :data="{type: key}"
                :fileList="[{url: form.value[key] ? form.value[key].indexOf('http') >= 0 ? form.value[key]: resUrl + form.value[key]: '', type: key}]"
                @imgUploadSuccess="imgUploadSuccess"
                @imgRemoveSuccess="imgRemoveSuccess"
              />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer">
        <el-button @click="dialog.edit = false">取消</el-button>
        <el-button type="primary" @click="edit" :loading="loading.edit">修改</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import * as utils from '@/utils/utils'
import * as ResponseStatus from '@/utils/response-status'
import UploadConfigImage from '@/components/Upload/UploadConfigImage'
import Tinymce from '@/components/Tinymce'
import JsonEditor from 'vue-json-editor'
const resUrl = process.env.VUE_APP_RES_URL
export default {
  name: 'SysConfigAddEdit',
  components: {
    UploadConfigImage,
    Tinymce,
    JsonEditor,
  },
  data() {
    return {
      dialog: {
        add: false,
        edit: false,
      },
      loading: {
        add: false,
        edit: false,
      },
      form: {
        name: undefined,
        value: undefined,
        comment: undefined,
        description: undefined,
        outsideAccess: undefined,
      },
      validateRules: {
        name: [
          {
            type: 'string',
            required: true,
            message: '此项为必须项',
            trigger: 'blur',
          },
          {
            type: 'string',
            min: 1,
            max: 50,
            message: '必须1-50个字符',
            trigger: 'blur',
          },
        ],
        value: [
          {
            type: 'string',
            required: true,
            message: '此项为必须项',
            trigger: 'blur',
          },
          {
            type: 'string',
            min: 1,
            max: 65535,
            message: '必须1-65535个字符',
            trigger: 'blur',
          },
        ],
        comment: [
          {
            type: 'string',
            min: 1,
            max: 65535,
            message: '必须1-65535个字符',
            trigger: 'blur',
          },
        ],
        description: [
          {
            type: 'string',
            min: 1,
            max: 50,
            message: '必须1-50个字符',
            trigger: 'blur',
          },
        ],
      },
      dataDictCodes: ['YES_NO'],
      selects: {
        outsideAccessSelect: [],
      },
      resUrl: resUrl,
    }
  },
  computed: {},
  mounted() {
    this.initDataDict()
  },
  methods: {
    initDataDict() {
      utils
        .getDataDict(this.dataDictCodes)
        .then((response) => {
          if (response) {
            utils.getSelectDictItems({
              mainComponent: this,
              dataDictItems: response,
              dictCode: 'YES_NO',
              formProp: 'outsideAccess',
              dataProp: 'outsideAccessSelect',
            })
          }
        })
        .catch((error) => {})
    },
    dialogClosed(formRef) {
      this.$refs[formRef].resetFields()
      for (let key in this.form) {
        this.form[key] = undefined
      }
    },
    add() {
      this.form.value = JSON.stringify(this.form.value)
      this.form.comment = JSON.stringify(this.form.comment)
      this.$emit('add')
    },
    edit() {
      this.form.value = JSON.stringify(this.form.value)
      this.form.comment = JSON.stringify(this.form.comment)
      this.$emit('edit')
    },
    /** 触发父组件展示关联表数据的搜索对话框 */
    showJoinSearch(options) {
      this.$emit('showJoinSearchDialog', options)
    },
    imgUploadSuccess(formProp, res) {
      this.form[formProp][res.message] = res.data
      this.$refs.JsonEditorValue.editor.set(this.form.value)
    },
    imgRemoveSuccess(formProp, type) {
      this.form[formProp][type] = ''
    },
  },
}
</script>

<style>
/** vue-json-editor的高度 */
div.jsoneditor {
  height: 250px;
}
</style>